package newui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlideUtil;
import com.NewActionHelp;
import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw.activity.AtlasActivity;
import com.shwread.android.qysw.activity.AtlasSeeActivity;
import com.shwread.android.qysw.activity.BaseBusinessActivity;
import com.shwread.android.qysw.activity.BaseSimpleActivity;
import com.shwread.android.qysw.activity.CommentListActivity;
import com.shwread.android.qysw.activity.ImageTextActivity;
import com.shwread.android.qysw.activity.NewBookDetailActivity;
import com.shwread.android.qysw.activity.PersonalInfoActivity;
import com.shwread.android.qysw.activity.VideoDetailActivity;
import com.shwread.android.qysw10000000.R;
import com.shwread.android.ui.customview.LinearListView;
import com.shwread.android.ui.widget.BookCoverView;
import com.shwread.android.ui.widget.refresh.PullToRefreshScrollView;
import com.shwread.android.ui.widget.refresh.PullToRefreshUtil;
import com.shwread.android.ui.widget.refresh.base.PullToRefreshBase;
import com.shwread.http.base.ObjectHttpResponseHandler;
import com.shwread.httpsdk.http.callback.BaseAction;
import com.shwread.httpsdk.http.face.QryCommentPraiseNumAction;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import logic.bean.AudioAndVideo;
import logic.bean.BookInfo;
import logic.bean.ImageCollection;
import logic.bean.MultiNews;
import logic.bean.NumberInfo;
import logic.bean.RegionContentItemBean;
import logic.bean.SingleBook;
import logic.bean.SingleNews;
import logic.constants.DefaultConsts;
import logic.util.DateUtil;
import logic.util.Util;

/**
 * Created by lex on 2017/7/13.
 */

public class EnterpriseModelContentActivity extends BaseSimpleActivity implements LinearListView.OnItemClickListener{

    private TextView tvTitle;
    private ImageView ivPersion;

    private LinearListView linearLv;
    private PullToRefreshScrollView refreshView;
    private int pageSize = 10;
    private boolean isHasMore = true;//true是还有更多数据，FALSE是没有更多
    private long currentPublishTime = 0;
    private int pageNum = 1;
    private InfoAdapter mContentAdapter;
    private Button btnBack;

    private long modelId = 0;
    private String modelName;
    private TextView empty;

    private final static int SINGLE_NEWS_TYPE = 1;
    private final static int MULTI_NEWS_TYPE = 2;
    private final static int AUDIO_NEWS_TYPE = 3;
    private final static int VIDEO_NEWS_TYPE = 4;
    private final static int IMAGE_SET_TYPE = 5;
    private final static int SINGLE_BOOK_TYPE = 6;
    private static final char REGION_SPLITOR = 8;
    public static final int IMAGE_COUNT = 5;

    private List<RegionContentItemBean> mContentItemList ;
    private TextView tv_comments_count;
    private String targetType;
    private String targetValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enterprise_model_content);
        Intent intent = getIntent();
        modelId = intent.getLongExtra("modelId",-1);
        modelName = intent.getStringExtra("modelName");
        if (modelId==-1)return;
        initView();
    }

    void initView() {
        mContentItemList = new ArrayList<>();
        linearLv = (LinearListView) findViewById(R.id.linearLv);
        refreshView = (PullToRefreshScrollView) findViewById(R.id.refreshView);
        tvTitle = (TextView) findViewById(R.id.common_header_name);
        empty = (TextView) findViewById(R.id.tv_empty_content);
        empty.setVisibility(View.GONE);
        tvTitle.setText(modelName);
        ivPersion = (ImageView) findViewById(R.id.common_header_persion);
        btnBack = (Button) findViewById(R.id.common_header_back);
        btnBack.setVisibility(View.VISIBLE);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EnterpriseModelContentActivity.this.finish();
            }
        });
        ivPersion.setVisibility(View.GONE);
        refreshView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                if (refreshView.isHeaderShown()) { //下拉刷新
                    pageNum = 1;
                    currentPublishTime = 0;
                    getContentDataById(modelId, currentPublishTime);
                } else if (refreshView.isFooterShown()) { //上拉加载
                    if (!isHasMore) {
                        PullToRefreshUtil.setLoadLabel(refreshView, "没有更多数据了");
                        refreshView.onRefreshComplete();
                    } else {
                        pageNum++;
                        getContentDataById(modelId, currentPublishTime);
                    }
                }
            }
        });
        mContentAdapter = new InfoAdapter(this,mContentItemList);
        linearLv.setAdapter(mContentAdapter);
        linearLv.setOnItemClickListener(this);
        getContentDataById(modelId,currentPublishTime);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode== BaseBusinessActivity.COMMENTS_REQUEST_CODE) {
            new QryCommentPraiseNumAction(EnterpriseModelContentActivity.this, targetType, targetValue, new BookCommentsNumberActionListener()).start();
        }
    }

    private void getContentDataById(final long modelId, final long publishTime) {
        NewActionHelp.qryModuleContentList(this, modelId, publishTime, pageSize, new ObjectHttpResponseHandler<Map<String, List<RegionContentItemBean>>>() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("111111Fail", "qryModuleContentList" + s);
                refreshView.onRefreshComplete();
                linearLv.setVisibility(View.GONE);
//                empty.setVisibility(View.VISIBLE);
            }

            @Override
            public void onSuccess(int i, Header[] headers, Map<String, List<RegionContentItemBean>> stringListMap, String s) {
                Log.e("111111OK", "qryModuleContentList" + s);
                Log.e("当前的id和时间", modelId + "===" + publishTime);
                linearLv.setVisibility(View.VISIBLE);
                empty.setVisibility(View.GONE);
                refreshView.onRefreshComplete();
                List<RegionContentItemBean> contentItems = stringListMap.get("modelContentList");
                if (null==contentItems||contentItems.size()==0){
                    if (pageNum==1){
                        empty.setVisibility(View.VISIBLE);
                    }else{
                        empty.setVisibility(View.GONE);
                    }
                    return;
                }
                removeAudioAndVideo(contentItems);//临时移除音频的数据、去除为空的数据
                isHasMore = contentItems.size() == pageSize;
                if (pageNum == 1) {//刷新
                    mContentItemList.clear();
                    mContentItemList.addAll(contentItems);
                } else {//取下一页
                    mContentItemList.addAll(contentItems);
                }
                RegionContentItemBean lastItem = mContentItemList.get(mContentItemList.size() - 1);
                currentPublishTime = lastItem.getPublishTime();
                mContentAdapter.notifyDataSetChanged();
            }

            @Override
            public Type getType() {
                return new TypeToken<Map<String, List<RegionContentItemBean>>>() {
                }.getType();
            }
        });
    }

    /**
     * 过滤掉音频文件和为空的文件
     * @param contentItems
     */
    private void removeAudioAndVideo(List<RegionContentItemBean> contentItems) {
        if (contentItems == null || contentItems.size() == 0) return;
        for (int i = 0; i < contentItems.size(); i++) {
            RegionContentItemBean contentItem = contentItems.get(i);
            String type = contentItem.getType();
            String bean = contentItem.getDataJson();
            if (null == type || type.equalsIgnoreCase("3") || null == bean || bean.length() == 0) {
                contentItems.remove(contentItem);
            }
        }
    }
    public void goPersonalActivity() {
        Intent intent = new Intent(this, PersonalInfoActivity.class);
        startActivity(intent);
    }

    @Override
    public void onItemClick(Object item, View view, int position) {
        tv_comments_count = (TextView) view.findViewById(R.id.tv_comments_count);
        ImageView im_comments = (ImageView) view.findViewById(R.id.im_comments);
        RegionContentItemBean contentItem = mContentItemList.get(position);
        Object objTarget = im_comments.getTag();
        if (objTarget == null) return;
        String strTarget = (String) objTarget;
        int splitorPos = strTarget.indexOf(REGION_SPLITOR);
        targetType = strTarget.substring(0, splitorPos);
        targetValue = strTarget.substring(splitorPos + 1);

        String contentType = contentItem.getType();
        if (contentType.equalsIgnoreCase("1")) { //单条资讯
            //todo 跳转到图文详情
            SingleNews singleNews = contentItem.getSingleNews();
            gotoImageTextActivity(singleNews.getHtmlUrl(), singleNews.getTitle(), singleNews.getModelContentDetailId() + "");
        } else if (contentType.equalsIgnoreCase("2")) {//多条资讯
            //每个已经处理了， 这里不需要处理
        } else if (contentType.equalsIgnoreCase("3")) {//音频

        } else if (contentType.equalsIgnoreCase("4")) {//视频
            gotoVideoDetailActivity(contentItem);
        } else if (contentType.equalsIgnoreCase("5")) { //图集
            //todo 跳转到查看图集图片
            gotoImageViewerActivity(position/*,2*/);
        } else if (contentType.equalsIgnoreCase("6")) { //单本书籍
            //跳转到书籍详情
            SingleBook singleBook = contentItem.getSingleBook();
            BookInfo bookInfo = singleBook.getBookInfo();
            if (bookInfo == null) return;
            //#ifdef SHOW_OLDBOOKDETAIL
            //$ Intent intent = new Intent(bActivity, BookDetailActivity.class);
            //#else
            Intent intent = new Intent(EnterpriseModelContentActivity.this, NewBookDetailActivity.class);
            //#endif
            bookInfo.setAuthor("");
            intent.putExtra("BookInfo", bookInfo);
            startActivityForResult(intent, BaseBusinessActivity.COMMENTS_REQUEST_CODE);
        }
    }

    /**
     * 视频资讯的详情页
     */
    private void gotoVideoDetailActivity(RegionContentItemBean contentItemBean) {
        Intent intent = new Intent(EnterpriseModelContentActivity.this, VideoDetailActivity.class);
        intent.putExtra("video", contentItemBean);
        startActivityForResult(intent, BaseBusinessActivity.COMMENTS_REQUEST_CODE);
    }

    /**
     * 跳转到查看图集的页面
     *
     * @param position 1 为直接进入查看, 2 为可能要进入到瀑布流页面
     */
    private void gotoImageViewerActivity(int position/*, int type*/) {
        int imgCounts = 0;
        RegionContentItemBean contentItem = mContentItemList.get(position);
        ImageCollection imageSet = contentItem.getImageSet();
        imgCounts = imageSet.getImageCollectionNum();
        Intent intent;
        if (imgCounts == 0) {
            Toast.makeText(EnterpriseModelContentActivity.this, "图集已下架", Toast.LENGTH_SHORT).show();
            return;
        }
        if (/*type == 1 || ( type == 2 && */imgCounts <= IMAGE_COUNT/*) */) {
            //直接进入看图片
            intent = new Intent(EnterpriseModelContentActivity.this, AtlasSeeActivity.class);
            intent.putExtra(AtlasSeeActivity.FROM_TYPE, DefaultConsts.TAG_FROM_ENTERPRISE);
            intent.putExtra(AtlasSeeActivity.IMAGE_COUNT, imgCounts);
        } else {//进入到瀑布流页面
            intent = new Intent(EnterpriseModelContentActivity.this, AtlasActivity.class);
            intent.putExtra(AtlasActivity.ATLAS_NAME, imageSet.getTitle());
        }
        //todo  设置图集的数据
        intent.putExtra(AtlasActivity.ATLAS_ID, imageSet.getImageCollectionId());
        startActivityForResult(intent, BaseBusinessActivity.COMMENTS_REQUEST_CODE);
    }

    /**
     * 跳转到图文详情页
     *
     * @param url
     */
    private void gotoImageTextActivity(String url, String title, String contentDetailId) {
        Intent intent = new Intent(EnterpriseModelContentActivity.this, ImageTextActivity.class);
        intent.putExtra(DefaultConsts.req_url_s, url);
        intent.putExtra(DefaultConsts.title_s, title);
        intent.putExtra(DefaultConsts.COMMENT_TARGET_TYPE, "2");
        intent.putExtra(DefaultConsts.COMMENT_TARGET_VALUE, contentDetailId);
        startActivityForResult(intent, BaseBusinessActivity.COMMENTS_REQUEST_CODE);
    }


    public class InfoAdapter extends BaseAdapter {

        private Context context;
        private List<RegionContentItemBean> mContentItemList ;

        public InfoAdapter(Context context,List<RegionContentItemBean> mContentItemList){
            this.context = context;
            this.mContentItemList = mContentItemList;
        }

        @Override
        public int getViewTypeCount() {
            return 6;
        }

        @Override
        public int getItemViewType(int position) {
            if (null != mContentItemList && position < mContentItemList.size()) {
                return Integer.parseInt(mContentItemList.get(position).getType());
            }
            return super.getItemViewType(position);
        }

        @Override
        public int getCount() {
            return mContentItemList.size();
        }

        @Override
        public Object getItem(int position) {
            return mContentItemList == null ? null : mContentItemList.get(position);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            if (view != null && view.getTag() != null && view.getTag() instanceof Mp4Holder) {
                ((Mp4Holder) view.getTag()).player_list_video.release();
            }
            final RegionContentItemBean contentItem = mContentItemList.get(position);
            LayoutInflater inflater = LayoutInflater.from(context);
            SingleinfoViewHolder singleInfo = null;
            MultiinfoViewHolder multiInfo = null;
            Mp4Holder mp4Holder = null;
            imagesetViewHolder imageset = null;
            SingleBookViewHolder singlebook = null;
            int contentType = Integer.parseInt(contentItem.getType());
            switch (contentType) {
                case SINGLE_NEWS_TYPE:
                    if (view == null) {
                        singleInfo = new SingleinfoViewHolder();
                        view = inflater.inflate(R.layout.region_singleinfo_item, null);
                        singleInfo.tv_info_title = (TextView) view.findViewById(R.id.tv_info_title);
                        singleInfo.tv_info_from = (TextView) view.findViewById(R.id.tv_info_from);
                        singleInfo.tv_item_introduce = (TextView) view.findViewById(R.id.tv_item_introduce);
                        singleInfo.im_bigimage = (ImageView) view.findViewById(R.id.im_bigimage);
                        singleInfo.im_leftimage = (ImageView) view.findViewById(R.id.im_leftimage);
                        singleInfo.im_rightimage = (ImageView) view.findViewById(R.id.im_rightimage);
                        singleInfo.tv_publishtime = (TextView) view.findViewById(R.id.tv_publishtime);
                        singleInfo.tv_comments_count = (TextView) view.findViewById(R.id.tv_comments_count);
                        singleInfo.im_comments = (ImageView) view.findViewById(R.id.im_comments);
                        view.setTag(singleInfo);
                    } else {
                        singleInfo = (SingleinfoViewHolder) view.getTag();
                    }
                    SingleNews singleNews = contentItem.getSingleNews();
                    singleInfo.tv_info_title.setText(singleNews.getTitle());
                    String fromSource = singleNews.getFromSource();
                    if (Util.isEmpty(fromSource)) {
                        singleInfo.tv_info_from.setVisibility(View.GONE);
                    } else {
                        singleInfo.tv_info_from.setText("来源：" + fromSource);
                    }
                    if (Util.isEmpty(singleNews.getShortDesc())) {
                        singleInfo.tv_item_introduce.setVisibility(View.GONE);
                    } else {
                        singleInfo.tv_item_introduce.setText(singleNews.getShortDesc());
                    }
                    int layoutType = singleNews.getLayoutType();
                    if (layoutType == 1) {//无图
                        singleInfo.im_bigimage.setVisibility(View.GONE);
                        singleInfo.im_leftimage.setVisibility(View.GONE);
                        singleInfo.im_rightimage.setVisibility(View.GONE);
                    } else if (layoutType == 2) {//大图
                        GlideUtil.load(context, singleNews.getCoverImageUrl(), R.drawable.bg_big_image, singleInfo.im_bigimage);
                        singleInfo.im_leftimage.setVisibility(View.GONE);
                        singleInfo.im_rightimage.setVisibility(View.GONE);
                    } else if (layoutType == 3) {//小左图
                        singleInfo.im_bigimage.setVisibility(View.GONE);
                        GlideUtil.load(context, singleNews.getCoverImageUrl(), R.drawable.bg_small_image, singleInfo.im_leftimage);
                        singleInfo.im_rightimage.setVisibility(View.GONE);
                    } else if (layoutType == 4) {//小右图
                        singleInfo.im_bigimage.setVisibility(View.GONE);
                        singleInfo.im_leftimage.setVisibility(View.GONE);
                        GlideUtil.load(context, singleNews.getCoverImageUrl(), R.drawable.bg_big_image, singleInfo.im_rightimage);

                    }
                    singleInfo.tv_publishtime.setText(DateUtil.getRegionTime(contentItem.getPublishTime()));
                    singleInfo.tv_comments_count.setText(singleNews.getCommentNum() + "");
                    singleInfo.im_comments.setTag("2" + REGION_SPLITOR + singleNews.getModelContentDetailId());//专区内容的资讯
                    singleInfo.im_comments.setOnClickListener(commentsOnclickListener);
//                    view.setTag(singleNews.getHtmlUrl());
                    break;
                case MULTI_NEWS_TYPE:
                    if (view == null) {
                        view = inflater.inflate(R.layout.region_multiinfo_item, null);
                        multiInfo = new MultiinfoViewHolder();
                        multiInfo.tv_biginfo_title = (TextView) view.findViewById(R.id.tv_biginfo_title);
                        multiInfo.tv_smallinfo1 = (TextView) view.findViewById(R.id.tv_smallinfo1);
                        multiInfo.tv_smallinfo2 = (TextView) view.findViewById(R.id.tv_smallinfo2);
                        multiInfo.tv_smallinfo3 = (TextView) view.findViewById(R.id.tv_smallinfo3);

                        multiInfo.tv_splitor1 = (TextView) view.findViewById(R.id.tv_splitor1);
                        multiInfo.tv_splitor2 = (TextView) view.findViewById(R.id.tv_splitor2);
                        multiInfo.tv_splitor3 = (TextView) view.findViewById(R.id.tv_splitor3);

                        multiInfo.im_bigimage = (ImageView) view.findViewById(R.id.im_bigimage);
                        multiInfo.im_smallimage1 = (ImageView) view.findViewById(R.id.im_smallimage1);
                        multiInfo.im_smallimage2 = (ImageView) view.findViewById(R.id.im_smallimage2);
                        multiInfo.im_smallimage3 = (ImageView) view.findViewById(R.id.im_smallimage3);

                        multiInfo.fl_biginfo = view.findViewById(R.id.fl_biginfo);
                        multiInfo.rl_smallinfo1 = view.findViewById(R.id.rl_smallinfo1);
                        multiInfo.rl_smallinfo2 = view.findViewById(R.id.rl_smallinfo2);
                        multiInfo.rl_smallinfo3 = view.findViewById(R.id.rl_smallinfo3);

                        multiInfo.tv_publishtime = (TextView) view.findViewById(R.id.tv_publishtime);
                        view.setTag(multiInfo);
                    } else {
                        multiInfo = (MultiinfoViewHolder) view.getTag();
                    }
                    MultiNews multiNews = contentItem.getMutilNews();
                    int count = multiNews.getNewsCount();
                    for (int i = 0; i < count; i++) {
                        setMultiNewsContent(position, i, multiInfo);
                    }
                    multiInfo.tv_publishtime.setText(DateUtil.getRegionTime(contentItem.getPublishTime()));
                    break;
                case AUDIO_NEWS_TYPE:

                    break;
                case VIDEO_NEWS_TYPE:
                    JCVideoPlayer.clearSavedProgress(context, null);
                    if (view != null && view.getTag() != null && view.getTag() instanceof Mp4Holder) {
                        mp4Holder = (Mp4Holder) view.getTag();
                    } else {
                        view = inflater.inflate(R.layout.item_lv_video, null);
                        mp4Holder = new Mp4Holder();
                        mp4Holder.player_list_video = (JCVideoPlayerStandard) view.findViewById(R.id.player_list_video);
                        mp4Holder.tv_video_desc = (TextView) view.findViewById(R.id.tv_video_desc);
                        mp4Holder.tv_video_title = (TextView) view.findViewById(R.id.tv_video_title);
                        mp4Holder.tv_publishtime = (TextView) view.findViewById(R.id.tv_publishtime);
                        mp4Holder.tv_comments_count = (TextView) view.findViewById(R.id.tv_comments_count);
                        mp4Holder.im_comments = (ImageView) view.findViewById(R.id.im_comments);
                        mp4Holder.player_list_video.tinyBackImageView.setVisibility(View.GONE);
                        mp4Holder.player_list_video.backButton.setVisibility(View.GONE);
                        mp4Holder.player_list_video.titleTextView.setVisibility(View.GONE);
                        view.setTag(mp4Holder);
                    }
                    AudioAndVideo audioAndVideo = contentItem.getVideoNews();
                    if (null == audioAndVideo.getTitle()) {
                        audioAndVideo.setTitle("");
                    }
                    mp4Holder.player_list_video.setUp(
                            audioAndVideo.getHtmlUrl(),
                            JCVideoPlayer.SCREEN_LAYOUT_LIST,
                            audioAndVideo.getTitle(), false);
                    Glide.with(context)
                            .load(audioAndVideo.getCoverImageUrl())
                            .placeholder(R.drawable.bg_big_image)
                            .error(R.drawable.bg_big_image)
                            .into(mp4Holder.player_list_video.thumbImageView);
                    mp4Holder.player_list_video.thumbImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    if (Util.isEmpty(audioAndVideo.getTitle())) {
                        mp4Holder.tv_video_title.setText("");
                    } else {
                        mp4Holder.tv_video_title.setText(audioAndVideo.getTitle());
                    }
                    if (Util.isEmpty(audioAndVideo.getShortDesc())) {
                        mp4Holder.tv_video_desc.setText("");
                    } else {
                        mp4Holder.tv_video_desc.setText(audioAndVideo.getShortDesc());
                    }
                    mp4Holder.tv_publishtime.setText(DateUtil.getRegionTime(contentItem.getPublishTime()));
                    mp4Holder.tv_comments_count.setText(audioAndVideo.getCommentNum() + "");
                    mp4Holder.player_list_video.thumbImageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            gotoVideoDetailActivity(contentItem);
                        }
                    });
                    mp4Holder.im_comments.setTag("2" + REGION_SPLITOR + audioAndVideo.getModelContentDetailId());//视频
                    mp4Holder.im_comments.setOnClickListener(commentsOnclickListener);
                    break;
                case IMAGE_SET_TYPE:
                    if (view == null) {
                        view = inflater.inflate(R.layout.region_imageset_item, null);
                        imageset = new imagesetViewHolder();
                        imageset.tv_imageset_title = (TextView) view.findViewById(R.id.tv_imageset_title);
                        imageset.tv_imageset_introduce = (TextView) view.findViewById(R.id.tv_imageset_introduce);

                        imageset.im_imageset_image3 = (ImageView) view.findViewById(R.id.im_imageset_image3);
                        imageset.im_imageset_image2 = (ImageView) view.findViewById(R.id.im_imageset_image2);
                        imageset.im_imageset_image1 = (ImageView) view.findViewById(R.id.im_imageset_image1);

                        imageset.tv_publishtime = (TextView) view.findViewById(R.id.tv_publishtime);
                        imageset.tv_comments_count = (TextView) view.findViewById(R.id.tv_comments_count);
                        imageset.im_comments = (ImageView) view.findViewById(R.id.im_comments);

                        imageset.fl_imageset = (FrameLayout) view.findViewById(R.id.fl_imageset);
                        imageset.fl_imageset.setTag(position);
                        view.setTag(imageset);
                    } else {
                        imageset = (imagesetViewHolder) view.getTag();
                    }
                    imageset.fl_imageset.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Object objUrl = v.getTag();
                            if (objUrl == null) return;
                            int pos = (Integer) objUrl;
                            //todo 直接跳转到查看图片集
                            gotoImageViewerActivity(pos/*,2*/);
                        }
                    });
                    ImageCollection imageSet = contentItem.getImageSet();
                    imageset.tv_imageset_title.setText(imageSet.getTitle());
                    imageset.tv_imageset_introduce.setText(imageSet.getShortDesc());
                    GlideUtil.load(context, imageSet.getCoverImageUrl(), R.drawable.bg_big_image, imageset.im_imageset_image1);
                    imageset.tv_publishtime.setText(DateUtil.getRegionTime(contentItem.getPublishTime()));
                    imageset.tv_comments_count.setText(imageSet.getCommentNum() + "");
                    imageset.im_comments.setTag("4" + REGION_SPLITOR + imageSet.getImageCollectionId());//图集
                    imageset.im_comments.setOnClickListener(commentsOnclickListener);
                    break;
                case SINGLE_BOOK_TYPE:
                    if (view == null) {
                        view = inflater.inflate(R.layout.region_singlebook_item, null);
                        singlebook = new SingleBookViewHolder();
                        singlebook.tv_book_title = (TextView) view.findViewById(R.id.tv_book_title);
                        singlebook.tv_book_name = (TextView) view.findViewById(R.id.tv_book_name);
                        singlebook.tv_book_author = (TextView) view.findViewById(R.id.tv_book_author);
                        singlebook.tv_book_lookedcount = (TextView) view.findViewById(R.id.tv_book_lookedcount);
                        singlebook.tv_book_introduce = (TextView) view.findViewById(R.id.tv_book_introduce);
                        singlebook.im_book_cover = (BookCoverView) view.findViewById(R.id.im_book_cover);
                        singlebook.tv_publishtime = (TextView) view.findViewById(R.id.tv_publishtime);
                        singlebook.tv_comments_count = (TextView) view.findViewById(R.id.tv_comments_count);
                        singlebook.im_comments = (ImageView) view.findViewById(R.id.im_comments);
                        view.setTag(singlebook);
                    } else {
                        singlebook = (SingleBookViewHolder) view.getTag();
                    }
                    SingleBook singleBook = contentItem.getSingleBook();
                    singlebook.tv_book_title.setText(singleBook.getTitle());
                    BookInfo bookInfo = singleBook.getBookInfo();
                    if (bookInfo != null) {
                        singlebook.tv_book_name.setText(bookInfo.getName());
                        singlebook.tv_book_author.setText("作者: " + bookInfo.getAuthor());
                        String introduce = singleBook.getShortDesc();
                        if (Util.isEmpty(introduce)) {
                            introduce = bookInfo.getEditorRecommend();
                            if (Util.isEmpty(introduce)) {
                                introduce = bookInfo.getShortIntro();
                            }
                        }
                        singlebook.tv_book_introduce.setText(introduce);
                        singlebook.im_book_cover.setImageUrl(bookInfo.getLogoUrl(), bookInfo.getCoverTagPicUrl());
                        singlebook.tv_publishtime.setText(DateUtil.getRegionTime(contentItem.getPublishTime()));
                        singlebook.tv_comments_count.setText(singleBook.getCommentNum() + "");
                        singlebook.im_comments.setTag("1" + REGION_SPLITOR + bookInfo.getBookId());//书籍
                        singlebook.im_comments.setOnClickListener(commentsOnclickListener);
                    }
                    break;
            }
            return view;
        }

        private  class SingleinfoViewHolder {
            TextView tv_publishtime, tv_info_title, tv_info_from, tv_item_introduce;
            ImageView im_bigimage, im_leftimage, im_rightimage;
            TextView tv_comments_count;
            ImageView im_comments;
        }

        private  class MultiinfoViewHolder {
            TextView tv_publishtime, tv_biginfo_title, tv_smallinfo1, tv_smallinfo2, tv_smallinfo3;
            TextView tv_splitor1, tv_splitor2, tv_splitor3;
            ImageView im_bigimage, im_smallimage1, im_smallimage2, im_smallimage3;
            View fl_biginfo;
            View rl_smallinfo1, rl_smallinfo2, rl_smallinfo3;
        }

        private  class SingleBookViewHolder {
            TextView tv_publishtime, tv_book_title, tv_book_name, tv_book_author, tv_book_lookedcount, tv_book_introduce;
            BookCoverView im_book_cover;
            TextView tv_comments_count;
            ImageView im_comments;
        }

        private  class imagesetViewHolder {
            TextView tv_publishtime, tv_imageset_title, tv_imageset_introduce;
            ImageView im_imageset_image3, im_imageset_image2, im_imageset_image1;
            FrameLayout fl_imageset;
            TextView tv_comments_count;
            ImageView im_comments;
        }

        private  class Mp4Holder {
            JCVideoPlayerStandard player_list_video;
            TextView tv_video_desc;
            TextView tv_video_title;
            ImageView im_comments;
            TextView tv_publishtime;
            TextView tv_comments_count;
        }


        View.OnClickListener commentsOnclickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object objTarget = v.getTag();
                if (objTarget == null) return;
                String strTarget = (String) objTarget;
                int splitorPos = strTarget.indexOf(REGION_SPLITOR);
                targetType = strTarget.substring(0, splitorPos);
                targetValue = strTarget.substring(splitorPos + 1);
                RelativeLayout parent = (RelativeLayout) v.getParent();
                tv_comments_count = (TextView) parent.findViewById(R.id.tv_comments_count);
                Intent intent1 = new Intent();
                intent1.setClass(EnterpriseModelContentActivity.this, CommentListActivity.class);
                intent1.putExtra(DefaultConsts.COMMENT_TARGET_TYPE, targetType);
                intent1.putExtra(DefaultConsts.COMMENT_TARGET_VALUE, targetValue);
                startActivityForResult(intent1, BaseBusinessActivity.COMMENTS_REQUEST_CODE);
            }
        };

        View.OnClickListener multiIfoOnclickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object objPositions = v.getTag();
                if (objPositions == null) return;
                String positions = (String) objPositions;
                int splitorPos = positions.indexOf(REGION_SPLITOR);
                int position1 = Integer.parseInt(positions.substring(0, splitorPos));
                int position2 = Integer.parseInt(positions.substring(splitorPos + 1));
                //todo 跳转到图文详情
                //gotoImageTextActivity(url, title);
                RegionContentItemBean contentItem = mContentItemList.get(position1);
                MultiNews multiNews = contentItem.getMutilNews();
                String contentType = multiNews.getNewsType(position2);
                if (contentType.equalsIgnoreCase("1")) { //单条资讯
                    //todo 跳转到图文详情
                    SingleNews singleNews = (SingleNews) multiNews.getNewsObject(position2);
                    gotoImageTextActivity(singleNews.getHtmlUrl(), singleNews.getTitle(), singleNews.getModelContentDetailId() + "");
                } else if (contentType.equalsIgnoreCase("3")) {//音频
                    // 跳转到图文详情
                    AudioAndVideo audioNews = (AudioAndVideo) multiNews.getNewsObject(position2);
                    gotoImageTextActivity(audioNews.getHtmlUrl(), audioNews.getTitle(), audioNews.getModelContentDetailId() + "");
                } else if (contentType.equalsIgnoreCase("4")) {//视频
                    // 跳转到图文详情
                    AudioAndVideo videoNews = (AudioAndVideo) multiNews.getNewsObject(position2);
                    gotoImageTextActivity(videoNews.getHtmlUrl(), videoNews.getTitle(), videoNews.getModelContentDetailId() + "");
                } else if (contentType.equalsIgnoreCase("5")) { //图集
                    //todo 跳转到查看图集图片
                    ImageCollection imageSet = (ImageCollection) multiNews.getNewsObject(position2);
                    int imgCounts = imageSet.getImageCollectionNum();
                    Intent intent;
                    if (imgCounts == 0) {
                        Toast.makeText(context, "图集已下架", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (imgCounts <= IMAGE_COUNT) {
                        //直接进入看图片
                        intent = new Intent(context, AtlasSeeActivity.class);
                        intent.putExtra(AtlasSeeActivity.FROM_TYPE, DefaultConsts.TAG_FROM_ENTERPRISE);
                        intent.putExtra(AtlasSeeActivity.IMAGE_COUNT, imgCounts);
                    } else {//进入到瀑布流页面
                        intent = new Intent(context, AtlasActivity.class);
                        intent.putExtra(AtlasActivity.ATLAS_NAME, imageSet.getTitle());
                    }
                    //设置图集的数据
                    intent.putExtra(AtlasActivity.ATLAS_ID, imageSet.getImageCollectionId());
                    context.startActivity(intent);

                } else if (contentType.equalsIgnoreCase("6")) { //单本书籍
                    //跳转到书籍详情
                    SingleBook singleBook = (SingleBook) multiNews.getNewsObject(position2);
                    BookInfo bookInfo = singleBook.getBookInfo();
                    if (bookInfo == null) return;
                    //#ifdef SHOW_OLDBOOKDETAIL
                    //$ Intent intent = new Intent(context, BookDetailActivity.class);
                    //#else
                    Intent intent = new Intent(context, NewBookDetailActivity.class);
                    //#endif
                    bookInfo.setAuthor("");
                    intent.putExtra("BookInfo", bookInfo);
                    context.startActivity(intent);
                }
            }
        };

        /**
         * 视频资讯的详情页
         */
        private void gotoVideoDetailActivity(RegionContentItemBean contentItemBean) {
            Intent intent = new Intent(context, VideoDetailActivity.class);
            intent.putExtra("video", contentItemBean);
            context.startActivity(intent);
        }

        /**
         * 跳转到查看图集的页面
         *
         * @param position 1 为直接进入查看, 2 为可能要进入到瀑布流页面
         */
        private void gotoImageViewerActivity(int position/*, int type*/) {
            int imgCounts = 0;
            RegionContentItemBean contentItem = mContentItemList.get(position);
            ImageCollection imageSet = contentItem.getImageSet();
            imgCounts = imageSet.getImageCollectionNum();
            Intent intent;
            if (imgCounts == 0) {
                Toast.makeText(context, "图集已下架", Toast.LENGTH_SHORT).show();
                return;
            }
            if (/*type == 1 || ( type == 2 && */imgCounts <= IMAGE_COUNT/*) */) {
                //直接进入看图片
                intent = new Intent(context, AtlasSeeActivity.class);
                intent.putExtra(AtlasSeeActivity.FROM_TYPE, DefaultConsts.TAG_FROM_ENTERPRISE);
                intent.putExtra(AtlasSeeActivity.IMAGE_COUNT, imgCounts);
            } else {//进入到瀑布流页面
                intent = new Intent(context, AtlasActivity.class);
                intent.putExtra(AtlasActivity.ATLAS_NAME, imageSet.getTitle());
            }
            //todo  设置图集的数据
            intent.putExtra(AtlasActivity.ATLAS_ID, imageSet.getImageCollectionId());
            context.startActivity(intent);
        }

        /**
         * 跳转到图文详情页
         *
         * @param url
         */
        private void gotoImageTextActivity(String url, String title, String contentDetailId) {
            Intent intent = new Intent(context, ImageTextActivity.class);
            intent.putExtra(DefaultConsts.req_url_s, url);
            intent.putExtra(DefaultConsts.title_s, title);
            intent.putExtra(DefaultConsts.COMMENT_TARGET_TYPE, "2");
            intent.putExtra(DefaultConsts.COMMENT_TARGET_VALUE, contentDetailId);
            context.startActivity(intent);
        }

        private void setMultiNewsContent(int position1, int position2, MultiinfoViewHolder multiInfo) {
            RegionContentItemBean contentItem = mContentItemList.get(position1);
            MultiNews multiNews = contentItem.getMutilNews();
            TextView tv_title = null, tv_splitor = null;
            ImageView im_image = null;
            View frame = null;

            if (position2 == 0) {
                frame = multiInfo.fl_biginfo;
                tv_title = multiInfo.tv_biginfo_title;
                im_image = multiInfo.im_bigimage;
            }

            if (position2 == 1) {
                tv_splitor = multiInfo.tv_splitor1;
                frame = multiInfo.rl_smallinfo1;
                tv_title = multiInfo.tv_smallinfo1;
                im_image = multiInfo.im_smallimage1;
            }
            if (position2 == 2) {
                tv_splitor = multiInfo.tv_splitor2;
                frame = multiInfo.rl_smallinfo2;
                tv_title = multiInfo.tv_smallinfo2;
                im_image = multiInfo.im_smallimage2;
            }
            if (position2 == 3) {
                tv_splitor = multiInfo.tv_splitor3;
                frame = multiInfo.rl_smallinfo3;
                tv_title = multiInfo.tv_smallinfo3;
                im_image = multiInfo.im_smallimage3;
            }

            int defaultImgRes;
            if (position2 != 0) {
                tv_splitor.setVisibility(View.VISIBLE);
                defaultImgRes = R.drawable.bg_small_image;

            } else {
                defaultImgRes = R.drawable.bg_big_image;
            }

            frame.setVisibility(View.VISIBLE);
            frame.setOnClickListener(multiIfoOnclickListener);
            String tagSring = "" + position1 + REGION_SPLITOR + position2;
            frame.setTag(tagSring);
            //根据不同的类型，可能填充不同的内容
            String DesStr = "";
            String contentType = multiNews.getNewsType(position2);
            if (contentType.equalsIgnoreCase("1")) { //单条资讯
                SingleNews singleNews = (SingleNews) multiNews.getNewsObject(position2);
                DesStr = singleNews.getTitle();
                tv_title.setText(DesStr);
//			im_image.setImageUrl(singleNews.getCoverImageUrl(), defaultImgRes);
                GlideUtil.load(context, singleNews.getCoverImageUrl(), defaultImgRes, im_image);
            } else if (contentType.equalsIgnoreCase("3")) {//音频
                AudioAndVideo audioNews = (AudioAndVideo) multiNews.getNewsObject(position2);
                DesStr = audioNews.getTitle();
                tv_title.setText(DesStr);
//			im_image.setImageUrl(audioNews.getCoverImageUrl(), defaultImgRes);
                GlideUtil.load(context, audioNews.getCoverImageUrl(), defaultImgRes, im_image);
            } else if (contentType.equalsIgnoreCase("4")) {//视频
                AudioAndVideo videoNews = (AudioAndVideo) multiNews.getNewsObject(position2);
                DesStr = videoNews.getTitle();
                tv_title.setText(DesStr);
//			im_image.setImageUrl(videoNews.getCoverImageUrl(), defaultImgRes);
                GlideUtil.load(context, videoNews.getCoverImageUrl(), defaultImgRes, im_image);
            } else if (contentType.equalsIgnoreCase("5")) { //图集
                ImageCollection imgsSet = (ImageCollection) multiNews.getNewsObject(position2);
                DesStr = imgsSet.getTitle();
                tv_title.setText(DesStr);
//			im_image.setImageUrl(imgsSet.getCoverImageUrl(), defaultImgRes);
                GlideUtil.load(context, imgsSet.getCoverImageUrl(), defaultImgRes, im_image);
            } else if (contentType.equalsIgnoreCase("6")) { //单本书籍
                SingleBook singleBook = (SingleBook) multiNews.getNewsObject(position2);
                DesStr = singleBook.getTitle();
                tv_title.setText(DesStr);
                BookInfo bookInfo = singleBook.getBookInfo();
                String url = "";
                if (bookInfo != null) {
                    url = bookInfo.getLogoUrl();
                }
                if (Util.isEmpty(url)) {
                    url = singleBook.getCoverImageUrl();
                }
//			im_image.setImageUrl(url, defaultImgRes);
                GlideUtil.load(context, url, defaultImgRes, im_image);
            }
        }
    }

    /**
     * 获取内容项评论数的 action listener
     */
    private class BookCommentsNumberActionListener
            extends
            BaseAction {
        @SuppressWarnings("unchecked")
        @Override
        public void OK(Object obj) {
            NumberInfo numbers = (NumberInfo) obj;
            final int commentsCount = numbers.commentNum;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv_comments_count.setText(commentsCount + "");
                }
            });
        }

        @Override
        public void onError() {
            super.onError();
            //通知 外部的调用者， 出错了。
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Util.showToast(EnterpriseModelContentActivity.this, "获取评论数失败");
                }
            });
        }
    }
}
